package com.GlobalPaint.app.ui.Mine;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.FankuiBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.codeBean;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FankuiActivity extends AppCompatActivity {
    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)
    TextView tvCommonCenter;
    @BindView(R.id.tv_fresh)
    TextView tvFresh;
    @BindView(R.id.ed_bt)
    EditText edBt;
    @BindView(R.id.et_fankui)
    EditText etFankui;
    private FankuiBean fankuiBean;
    private codeBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fankui);
        ButterKnife.bind(this);
        tvCommonCenter.setText("反馈");
        tvFresh.setVisibility(View.VISIBLE);
        tvFresh.setText("发布");
    }

    private void getFankui() {
        String url = Constants.JASON_SERVICE_URL + "/qlwfeedback/appsave";
        DataManager.getInst().postHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), fankuiBean, codeBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean = (codeBean) o;
                if (bean.getStatus() == 1) {
                    Toast.makeText(FankuiActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {

            }
        });
    }

    @OnClick({R.id.iv_goback, R.id.tv_fresh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_goback:
                finish();
                break;
            case R.id.tv_fresh:
                String bt=edBt.getText().toString();
                String neirong=etFankui.getText().toString();

                if(TextUtils.isEmpty(bt)){
                    Toast.makeText(FankuiActivity.this, "请输入反馈标题", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(neirong)){
                    Toast.makeText(FankuiActivity.this, "请输入反馈内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                fankuiBean=new FankuiBean();
                fankuiBean.setTitle(bt);
                fankuiBean.setContent(neirong);
                fankuiBean.setUserId(DataManager.userEntity.getUserId());
                getFankui();
                break;
        }
    }
    //隐藏软键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    // 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            return !(event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom);
        }
        return false;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
